<script setup>

import {Back, ChatDotSquare, ChatRound, Location, Message, Operation, User} from "@element-plus/icons-vue";
import {get, logout} from "@/net/index.js";
import router from "@/router/router.js";
import {useStore} from "@/store/store.js";
import {onMounted, ref} from "vue";

const store = useStore();
const userLogout = () => {
  logout(() => router.push("/"));
}

const loading = ref(true);
const getUserInfo = () => {
    get(`api/user/info`,(data) =>{
      store.user = data.data;
      console.log(data.data);
      loading.value = false;
      console.log(store.getAvatarURL);
    })
}

onMounted(() => {
  getUserInfo()
})
</script>

<template>
  <div class="main-container" v-loading="loading" element-loading-text="正在加载，请稍等...">
    <el-container style="height: 100%" v-if="!loading">
      <el-header class="main-container-header">
        <el-image src="https://element-plus.org/images/element-plus-logo.svg" alt="logo" class="logo"></el-image>
        <div style="flex: 1;" class="user-info">
          <div class="userinfo-profile">
            <!--   用户名和邮箱         -->
            <div>{{store.user.username}}</div>
            <div>{{!store.user.email ? '' :store.user.email}}</div>
          </div>
          <!--       头像       -->
          <el-dropdown>
            <el-avatar  :src="store.getAvatarURL" icon="el-icon-user"></el-avatar>
            <template #dropdown>
              <el-dropdown-item>
                <el-icon>
                  <Operation/>
                </el-icon>
                <span>个人中心</span><el-tag size="small">待开发</el-tag>
              </el-dropdown-item>
              <el-dropdown-item divided @click="userLogout()">
                <el-icon>
                  <Back/>
                </el-icon>
                <span>退出登录</span>
              </el-dropdown-item>
            </template>
          </el-dropdown>
        </div>
      </el-header>
      <el-container>
        <el-aside style="width: 230px">
          <el-scrollbar style="height: calc(100vh - 55px)">
            <el-menu
                style="min-height: calc(100vh - 55px)"
                router
                :default-active="$route.path"
                :default-openeds="['1','2']"
            >
              <el-sub-menu index="1">
                <template #title>
                  <el-icon>
                    <Location/>
                  </el-icon>
                  <span><b>校园论坛</b></span>
                </template>
                <el-menu-item index="/index">
                  <template #title>
                    <el-icon>
                      <ChatDotSquare/>
                    </el-icon>
                    <span>帖子广场</span>
                  </template>
                </el-menu-item>
                <el-menu-item index="/tiezi">
                  <template #title>
                    <el-icon>
                      <Message/>
                    </el-icon>
                    <span>我的帖子</span> <el-tag >暂未开放</el-tag>
                  </template>
                </el-menu-item>
              </el-sub-menu>
              <el-sub-menu index="2">
                <template #title>
                  <el-icon>
                    <Operation/>
                  </el-icon>
                  <span><b>个人设置</b></span>
                </template>
                <el-menu-item index="/index/user-setting">
                  <template #title>
                    <el-icon>
                      <User/>
                    </el-icon>
                    个人信息设置
                  </template>
                </el-menu-item>
              </el-sub-menu>
            </el-menu>
          </el-scrollbar>
        </el-aside>
        <el-main class="main-container-page">
          <el-scrollbar style="height: calc(100vh - 55px)">
            <router-view v-slot="{Component}">
              <transition name="el-fade-in-linear" mode="out-in">
                <component :is="Component" style="height: 100%"/>
              </transition>
            </router-view>
          </el-scrollbar>
        </el-main>
      </el-container>
    </el-container>

  </div>
</template>

<style scoped lang="less">

.main-container-header {
  border-bottom: 1px solid var(--el-border-color);
  height: 55px;
  display: flex;
  align-items: center;
  box-sizing: border-box;

  .logo {
    height: 32px
  }

  .user-info {
    display: flex;
    align-items: center;
    justify-content: flex-end;

    .el-avatar {
      cursor: pointer;
    }

    .userinfo-profile {
      text-align: center;
      margin-right: 20px;

      & > :first-child {
        font-size: 18px;
        font-weight: bold;
        line-height: 20px;
      }

      & > :last-child {
        font-size: 10px;
        color: grey;
      }
    }
  }

}

.main-container {
  width: 100vw;
  height: 100vh;
}
</style>